package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: liangWeiQUQ
 * @Description: TODO
 * @DateTime: 2021/3/1 16:02
 **/
@DubboService
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * @param
     * @Method
     * @Author 传闻中的梁大侠
     * @Version 1.0
     * @Description 批量上传excel设置预约人数
     * @Return
     * @Exception
     * @Date
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            orderSettingDao.insertSelective(orderSetting);
        }

    }

    /**
     * @param
     * @Method
     * @Author 传闻中的梁大侠
     * @Version 1.0
     * @Description 根据日期查询当月的预约信息
     * @Return`
     * @Exception
     * @Date
     */
    @Override
    public List<OrderSetting> findByDate(String startTime, String endTime) {
        //多个级别联合查询
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //SELECT * FROM `t_ordersetting` WHERE orderDate BETWEEN '2021-03-01' AND '2021-3-31'
        criteria.andBetween("orderDate", startTime, endTime);
        //SELECT * FROM `t_ordersetting` WHERE orderDate >='2021-03-01' AND orderDate<= '2021-3-31'
   /* criteria.andGreaterThanOrEqualTo("orderDate", startTime);
    criteria.andLessThanOrEqualTo("orderDate", endTime);*/
        return orderSettingDao.selectByExample(example);
    }

    /**
     * @param
     * @Method
     * @Author 传闻中的梁大侠
     * @Version 1.0
     * @Description 设置可预约人数
     * @Return
     * @Exception
     * @Date
     */
    @Override
    public void editNumByDate(OrderSetting orderSetting) {
        //1.根据日期查询预约 ---> select * from t_ordersetting where orderDate = "2021-03-06" and number=100
        OrderSetting orderSetting1 = new OrderSetting();
        orderSetting1.setOrderDate(orderSetting.getOrderDate());
        OrderSetting selectOne = orderSettingDao.selectOne(orderSetting1);
        System.out.println(selectOne);
        //2.如果不存在那么新增
        if(null == selectOne){
            orderSettingDao.insertSelective(orderSetting);
        }else {//3.如果存在---》判断可预约人数与已预约人数的关系
            //4.可预约人数<已预约人数--->抛出异常
            if(orderSetting.getNumber() < selectOne.getReservations()){
                throw new BusinessException("可预约人数不能小于已预约人数！");
            }
            //5.修改
            orderSetting.setId(selectOne.getId()); //此处需要注意必须设置主键
            orderSettingDao.updateByPrimaryKeySelective(orderSetting);
        }
    }
}
